#Basic starting point tests for the standard CRUD controller actions.  Include
#in your functional test and then set @scaffold_singular_name = 'resourcename' in your
#setup method, as this is evaled to get the AR classname, the named_routes, and
#the instance name of the controller assigned variable.  See
#link:../test/dynamic_scaffold_resource_test.rb for a sample of usage.  If you
#have session variables that must be set, like :user_id, then set 
#@scaffold_session_params in your set-up as well.  If your model has any
#validation that will fail on an empty {} then set @scaffold_dummy_object to
#get the create and update tests to pass.
module DynamicScaffoldResourceTestHelper
  @scaffold_singular_name = 'resource'
  @scaffold_session_params |= {}
  @scaffold_dummy_object |= {}

  #These tests were included and run, checks proper inclusion.
  def test_included
    assert true
  end
  
  def test_should_get_index
    get :index, {} , @scaffold_session_params
    assert_response :success
    assert assigns(@scaffold_singular_name.pluralize)
  end
  
  def test_should_get_new
    get :new, {}, @scaffold_session_params
    assert_response :success
  end  

  def test_should_get_show
    get :show, {:id => 1}, @scaffold_session_params
    assert_response :success
  end

  def test_should_get_edit
    get :edit, {:id => 1}, @scaffold_session_params
    assert_response :success
  end

  def test_should_create_resource
    resource_class = eval(@scaffold_singular_name.camelize)
    old_count = resource_class.count
    post :create, {@scaffold_singular_name.to_sym => @scaffold_dummy_object}, @scaffold_session_params
    assert_equal old_count+1, resource_class.count
    #assert_redirected_to resource_path(assigns(:resource)
    assert_redirected_to eval("#{@scaffold_singular_name}_path(assigns(:#{@scaffold_singular_name}))")
  end

  def test_should_update_resource
    put :update, {:id => 1, @scaffold_singular_name.to_sym => @scaffold_dummy_object}, @scaffold_session_params
    #assert_redirected_to resource_path(assigns(:resource)
    assert_redirected_to eval("#{@scaffold_singular_name}_path(assigns(:#{@scaffold_singular_name}))")
  end
  
  def test_should_destroy_resource
    resource_class = eval(@scaffold_singular_name.camelize)
    old_count = resource_class.count
    delete :destroy, {:id => 1}, @scaffold_session_params
    assert_equal old_count-1, resource_class.count
    
    #assert_redirected_to resources_path
    assert_redirected_to eval("#{@scaffold_singular_name.pluralize}_path")
  end
end